<?php
/**
 * Created by PhpStorm.
 * User: 庆金
 * Date: 2019-12-21
 * Time: 21:50
 * 子进程的实际应用
 */
echo "process-start-time:".date("Y-m-d H:i:s").PHP_EOL;
$works = [];
$urls = [
    'http://baidu.com',
    'http://sina.com.cn',
    'http://qq.com',
    'http://baidu.com?search=lexunw.com',
    'http://baidu.com?search=lexunw',
    'http://baidu.com?search=wht.net.cn',
];
for ($i=0;$i<6;$i++){
    $process = new swoole_process(function (swoole_process $work) use($i,$urls){
        $content = curlData($urls[$i]);
//        echo $content.PHP_EOL;
        $work->write($content.PHP_EOL);
    },true);
    $pid = $process->start();
    $works[$pid] = $process;
}
foreach ($works as $process){
    echo $process->read();
}

/**
 * 模拟请求url的内容
 * @param $url
 * @return string
 */
function curlData($url){
    //curl file_get_contents
    sleep(1);
    return $url.'sucdess'.PHP_EOL;
}

echo "process-start-end:".date("Y-m-d H:i:s").PHP_EOL;